package com.bzkj.web.controller.basicInfo;

import com.bzkj.basicinfo.domain.inventory.InventoryChangeRecord;
import com.bzkj.basicinfo.domain.inventory.dto.InventoryChangeQueryDto;
import com.bzkj.basicinfo.domain.inventory.vo.DrugSummaryVo;
import com.bzkj.basicinfo.domain.inventory.vo.HuoweiSummaryVo;
import com.bzkj.basicinfo.domain.inventory.vo.InventoryStatusVo;
import com.bzkj.basicinfo.service.inventory.InventoryChangeService;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.page.PageDomain;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.page.TableSupport;
import com.bzkj.common.constant.HttpStatus;
import com.bzkj.common.utils.poi.ExcelUtil;

import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存变更记录Controller
 * 
 * @author system
 * @date 2025-08-05
 */
@Api(tags = "库存变更记录管理")
@RestController
@RequestMapping("/basicInfo/inventoryChange")
public class InventoryChangeController extends BaseController {
    
    @Autowired
    private InventoryChangeService inventoryChangeService;
    
    /**
     * 查询库存状态详细列表（默认展示sphwph所有不为0的记录及其状态）
     */
    @ApiOperation("查询库存状态详细列表")
    @GetMapping("/inventoryStatusList")
    public TableDataInfo inventoryStatusList(InventoryChangeQueryDto queryDto) {
        // 不在这里设置分页，让Service层处理完整的业务逻辑后再分页
        List<InventoryStatusVo> allList = inventoryChangeService.selectInventoryStatusList(queryDto);

        // 手动处理分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();

        // 计算分页
        int total = allList.size();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        List<InventoryStatusVo> pageList;
        if (startIndex >= total) {
            pageList = new ArrayList<>();
        } else {
            pageList = allList.subList(startIndex, endIndex);
        }

        // 构造分页结果
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageList);
        rspData.setTotal(total);

        return rspData;
    }

    /**
     * 查询货位汇总信息列表
     */
    @ApiOperation("查询货位汇总信息列表")
    @GetMapping("/huoweiSummary")
    public TableDataInfo huoweiSummary(InventoryChangeQueryDto queryDto) {
        // 不在这里设置分页，让Service层处理完整的业务逻辑后再分页
        List<HuoweiSummaryVo> allList = inventoryChangeService.selectHuoweiSummary(queryDto);

        // 手动处理分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();

        // 计算分页
        int total = allList.size();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        List<HuoweiSummaryVo> pageList;
        if (startIndex >= total) {
            pageList = new ArrayList<>();
        } else {
            pageList = allList.subList(startIndex, endIndex);
        }

        // 构造分页结果
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageList);
        rspData.setTotal(total);

        return rspData;
    }
    
    /**
     * 查询指定货位下的商品汇总信息列表
     */
    @ApiOperation("查询指定货位下的商品汇总信息列表")
    @GetMapping("/drugSummary")
    public TableDataInfo drugSummary(InventoryChangeQueryDto queryDto) {
        startPage();
        List<DrugSummaryVo> list = inventoryChangeService.selectDrugSummary(queryDto);
        return getDataTable(list);
    }
    
    /**
     * 查询库存变更记录详情列表
     */
    @ApiOperation("查询库存变更记录详情列表")
    @GetMapping("/changeDetails")
    public TableDataInfo changeDetails(InventoryChangeQueryDto queryDto) {
        startPage();
        List<InventoryChangeRecord> list = inventoryChangeService.selectInventoryChangeList(queryDto);
        return getDataTable(list);
    }
    
    /**
     * 根据盘点号分析盘点异常情况
     */
    @ApiOperation("根据盘点号分析盘点异常情况")
    @GetMapping("/analyzeByInventoryId/{inventoryId}")
    public TableDataInfo analyzeByInventoryId(@PathVariable String inventoryId, InventoryChangeQueryDto queryDto) {
        // 不在这里设置分页，让Service层处理完整的业务逻辑后再分页
        List<HuoweiSummaryVo> allList = inventoryChangeService.analyzeInventoryByInventoryId(inventoryId, queryDto);

        // 手动处理分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();

        // 计算分页
        int total = allList.size();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        List<HuoweiSummaryVo> pageList;
        if (startIndex >= total) {
            pageList = new ArrayList<>();
        } else {
            pageList = allList.subList(startIndex, endIndex);
        }

        // 构造分页结果
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageList);
        rspData.setTotal(total);

        return rspData;
    }

    /**
     * 导出库存状态详细列表
     */
    @ApiOperation("导出库存状态详细列表")
    @PostMapping("/export/inventoryStatusList")
    @PreAuthorize("@ss.hasPermi('basicInfo:inventoryChange:export')")
    public void exportInventoryStatusList(HttpServletResponse response, InventoryChangeQueryDto queryDto) throws Exception {
        List<InventoryStatusVo> list = inventoryChangeService.selectInventoryStatusList(queryDto);
        ExcelUtil<InventoryStatusVo> util = new ExcelUtil<>(InventoryStatusVo.class);
        util.exportExcel(response, list, "库存状态列表", "库存状态");
    }

}
